package com.glsc.ngateway.opmanage.controller.ledger;

import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.User;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.ledger.LedgerAuthTemplate;
import com.glsc.ngateway.opmanage.aspect.UserCache;
import com.glsc.ngateway.opmanage.dto.LedgerAuthDto;
import com.glsc.ngateway.opmanage.service.ledger.LedgerAuthTemplateService;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 台账权限-台账模板配置
 */
@RestController
@RequestMapping("/api/ledger/auth/template")
@Slf4j
public class LedgerAuthTemplateController {

    @Resource
    private LedgerAuthTemplateService ledgerAuthTemplateService;

    @Resource
    private UserCache userCache;


    /**
     * 根据用户查询所有的岗位信息
     *
     * @param request
     * @return
     */
    @GetMapping("/listByUserId")
    public PlatformResponse<List<LedgerAuthTemplate>> listByUserId(HttpServletRequest request) {
        User user = userCache.get(request);
        return PlatformResponse.successData(ledgerAuthTemplateService.listByUserId(user.getUsername()));
    }

    /**
     * 分页查询
     *
     * @param obj
     * @return
     */
    @PostMapping("/page")
    public PlatformResponse<Page<LedgerAuthTemplate>> findClientPage(@RequestBody LedgerAuthDto obj) {

        Map<String, Object> param = new HashMap<>();

        if (StringUtils.isNotBlank(obj.getName())) {
            param.put("LIKE_name", obj.getName());
        }
        param.put("EQ_ledgerAuthId", obj.getLedgerAuthId());
        Page<LedgerAuthTemplate> page = ledgerAuthTemplateService.page(PageUtil.initPageNo(obj.getPageNo()), PageUtil.initPageSize(obj.getPageSize()), param);
        return PlatformResponse.successData(page);
    }

    /**
     * 根据di查询
     *
     * @param
     * @return
     */
    @GetMapping("/findById")
    public PlatformResponse<LedgerAuthTemplate> findById(@ApiParam(value = "id") @RequestParam(required = true) Integer id) {
        return PlatformResponse.successData(ledgerAuthTemplateService.findById(id));
    }

    /**
     * 保存
     *
     * @param obj
     * @return
     */
    @PostMapping("/save")
    public PlatformResponse<LedgerAuthTemplate> save(@RequestBody LedgerAuthTemplate obj, HttpServletRequest request) {
        User user = userCache.get(request);
        obj.setCreatorId(user.getId());
        LedgerAuthTemplate save = ledgerAuthTemplateService.save(obj);
        return PlatformResponse.successData(save);
    }

    /**
     * 删除接口
     *
     * @return
     */
    @PostMapping("/delete")
    public PlatformResponse<?> delete(@RequestBody LedgerAuthTemplate obj) {
        ledgerAuthTemplateService.deleteById(obj.getId());
        return PlatformResponse.success();
    }


}
